package com.refid.api.cmd;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import com.refid.api.util.BuleUtil;

public class YLCmd extends Cmd {
	public static final String CMD_START = "A0";// 命令开始

	@Override
	public HashSet<String> cmdTOEcp(List<String> list) {
		HashSet<String> set = new HashSet<String>();
		if (list == null || list.size() == 0)
			return set;
		String cmdType = null;
		String cmd = null;
		String ecp = null;
		for (int i = 0; i < list.size(); i++) {
			cmd = list.get(i);
			if (cmd.length() < 60)
				continue;
			cmdType = cmd.substring(3 * 3, 3 * 3 + 2);
			if ("8A".equals(cmdType) || "89".equals(cmdType)) {
				ecp = cmd.substring(3 * 7, cmd.length() - 3 * 2);
				if (ecp.length() > 34) {
					set.add(ecp);
				}
			}
		}
		return set;
	}

	@Override
	public List<String> cmds(String hexs) {
		List<String> list = new ArrayList<String>();
		hexs = BuleUtil.trimTONull(hexs);
		if (hexs == null)
			return list;
		int index = hexs.indexOf(CMD_START);
		String hexLen = null;
		String cmd = null;
		int len = 0;
		String chekSum = null;
		String chekSum_ = null;
		while (index != -1) {
			hexLen = hexs.substring(index + 3, index + 3 + 2);
			len = hexToInt(hexLen);
			cmd = hexs.substring(index, index + 2 + 3 + len * 3);
			chekSum = cmd.substring(cmd.length() - 2);
			chekSum_ = checkSum(cmd.substring(0, cmd.length() - 2));
			if (chekSum.equalsIgnoreCase(chekSum_)) {
				list.add(cmd);
			}
			index = hexs.indexOf(CMD_START, index + cmd.length());
		}
		return list;

	}

	@Override
	protected String repairCmd(String hexs) {
		hexs = BuleUtil.trimTONull(hexs);
		int len = hexs.split(" ").length + 1;
		String len_ = byteToHex((byte) len);
		String cmd = CMD_START + " " + len_ + " " + BuleUtil.trimTONull(hexs);
		cmd = cmd + " " + checkSum(cmd);
		return cmd;
	}

	@Override
	public String checkSum(String hexs) {
		if (BuleUtil.trimTONull(hexs) == null)
			return "";
		int cnt = 0;
		String hex = null;
		String[] args = hexs.split(" ");
		for (int i = 0; i < args.length; i++) {
			hex = args[i];
			if (hex == null)
				continue;
			cnt = cnt + Integer.parseInt(hex, 16);
		}
		cnt = (~cnt) + 1;
		hex = byteToHex((byte) cnt);
		return hex;
	}

	public static void main(String[] args) throws Exception {
		Cmd cmd = new YLCmd();
		System.out.println(cmd.repairCmd("01 8A 00 02 04 02 04 02 04 02 100 01"));
		System.out.println(cmd.repairCmd("01 89 FF"));
		System.out.println(cmd.repairCmd("01 72 "));// A0 03 01 72 EA
		System.out.println(cmd.repairCmd("01 72 08 02"));// A0 05 01 72 08 02 DE A0 05 01 72 08 02 DE
		System.out.println(cmd.repairCmd("01 7B 01 1D"));// A0 05 01 7B 01 1D C1
		System.out.println("========");
		String msg = "A0 04 01 74 10 D7 A0 13 01 89 A4 30 00 E2 00 00 19 66 0A 02 52 07 00 E3 0E 49 EF A0 0A 01 89 00 00 05 00 00 00 01 C6 A0 04 01 74 10 D7 A0 13 01 89 B4 30 00 E2 00 00 19 66 0A 02 52 07 00 E3 0E 4B DD A0 0A 01 89 00 00 07 00 00 00 01 C4 A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 13 01 89 BC 30 00 E2 00 00 19 66 0A 02 52 07 00 E3 0E 46 DA A0 0A 01 89 00 00 07 00 00 00 01 C4 A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 04 01 89 22 B0 A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 13 01 89 A4 30 00 E2 00 00 19 66 0A 02 52 07 00 E3 0E 4A EE A0 0A 01 89 00 00 07 00 00 00 01 C4 A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7 A0 13 01 89 A4 30 00 E2 00 00 19 66 0A 02 52 07 00 E3 0E 46 F2 A0 0A 01 89 00 00 05 00 00 00 01 C6 A0 04 01 74 10 D7 A0 0A 01 89 00 00 00 00 00 00 00 CC A0 04 01 74 10 D7";
		List<String> list = cmd.cmds(msg);
		System.out.println(BuleUtil.toString(list).replaceAll(",", "\n"));
		HashSet<String> set = cmd.cmdTOEcp(list);
		System.out.println(BuleUtil.toString(set).replaceAll(",", "\n"));
	}
}
